//axios是一个插件 ，可以发起网络请求，可以在node或浏览器中运行
const axios = require("axios").default;
const { cheerio } = require("cheerio");
const fs = require('fs')
//html解析,cheerio可以将html字符串转换为jquery对象
const cheerio = require("cheerio");
axios
  .get("http://www.fyrsks.com/")
  .then((res)=>{
    // console.log(res.data);
    /* fs.writeFileSync('./data/bqg.html',res.data);
    console.log('写入成功'); */
    const $ =cheerio.load(res.data);
    const books =[]
    $('.fl .txt-list li').each(function(){
      const book = {}
      book.name = $(this).find('.s2 a').text();
      book.author =$(this).find('.s4').text();
      book.link = $(this).find('.s2 a').attr("href");
      books.push(book);
    });
    console.log(books);
    fs.writeFileSync("./data/books.json",JSON.stringify(books));
    })
  .catch((err)=>{
    console.log(err);
  })

/* 
var strHtml = `
 <div>
 <h1>一级标题</h1>
 </div>
`
const $ = cheerio.load(strHtml);
console.log($("h1").text()); 
*/